home *** CD-ROM | disk | FTP | other *** search
/ Chip 2006 June (Extra) / CHIP 2006-06.3.iso / program / opensource / clamav-devel.exe / contrib / Windows / clamav.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2006-05-16  |  7.1 KB  |  278 lines

  1. // clamav.cpp : Defines the class behaviors for the application.
  2. //
  3.  
  4. /*
  5.  *  Copyright (C) 2004 Nigel Horne <njh@bandsman.co.uk>
  6.  *
  7.  *  This program is free software; you can redistribute it and/or modify
  8.  *  it under the terms of the GNU General Public License as published by
  9.  *  the Free Software Foundation; either version 2 of the License, or
  10.  *  (at your option) any later version.
  11.  *
  12.  *  This program is distributed in the hope that it will be useful,
  13.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  *  GNU General Public License for more details.
  16.  *
  17.  *  You should have received a copy of the GNU General Public License
  18.  *  along with this program; if not, write to the Free Software
  19.  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  20.  *  MA 02110-1301, USA.
  21.  */
  22. #include "stdafx.h"
  23. #include "resource.h"
  24.  
  25. #include "clamav.h"
  26.  
  27. #include "clamadoc.h"
  28. #include "clamavw.h"
  29.  
  30. #include <winsock.h>
  31.  
  32. #ifdef _DEBUG
  33. #undef THIS_FILE
  34. static char BASED_CODE THIS_FILE[] = __FILE__;
  35. #endif
  36.  
  37. /////////////////////////////////////////////////////////////////////////////
  38. // CClamavApp
  39.  
  40. BEGIN_MESSAGE_MAP(CClamavApp, CWinApp)
  41.     //{{AFX_MSG_MAP(CClamavApp)
  42.     ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
  43.     ON_COMMAND(ID_FILE_NEW, OnFileNew)
  44.     ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
  45.     ON_COMMAND(ID_SET_OPTIONS, OnSetOptions)
  46.     //}}AFX_MSG_MAP
  47.     // Standard file based document commands
  48.     // ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
  49.     ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
  50.     // Standard print setup command
  51.     ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
  52. END_MESSAGE_MAP()
  53.  
  54. /////////////////////////////////////////////////////////////////////////////
  55. // CClamavApp construction
  56.  
  57. CClamavApp::CClamavApp()
  58. {
  59.     // TODO: add construction code here,
  60.     // Place all significant initialization in InitInstance
  61. }
  62.  
  63. CClamavApp::~CClamavApp()
  64. {
  65. }
  66.  
  67. /////////////////////////////////////////////////////////////////////////////
  68. // The one and only CClamavApp object
  69.  
  70. CClamavApp theApp;
  71.  
  72. /////////////////////////////////////////////////////////////////////////////
  73. // CClamavApp initialization
  74.  
  75. BOOL CClamavApp::InitInstance()
  76. {
  77.     // Standard initialization
  78.     // If you are not using these features and wish to reduce the size
  79.     //  of your final executable, you should remove from the following
  80.     //  the specific initialization routines you do not need.
  81.  
  82.     Enable3dControls();
  83.  
  84.     LoadStdProfileSettings();  // Load standard INI file options (including MRU)
  85.  
  86.     // Register the application's document templates.  Document templates
  87.     //  serve as the connection between documents, frame windows and views.
  88.  
  89.     CSingleDocTemplate* pDocTemplate;
  90.     pDocTemplate = new CSingleDocTemplate(
  91.         IDR_MAINFRAME,
  92.         RUNTIME_CLASS(CClamavDoc),
  93.         RUNTIME_CLASS(CMainFrame),       // main SDI frame window
  94.         RUNTIME_CLASS(CClamavView));
  95.     AddDocTemplate(pDocTemplate);
  96.     
  97.     // Start up Winsock
  98.     WSAData wsaData;
  99.  
  100.     if(WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) {
  101.         AfxMessageBox("WSAStartup() failed error code");
  102.         return FALSE;
  103.     }
  104.     clamServer = NULL;
  105.  
  106.     if (m_lpCmdLine[0] != '\0')    {
  107.         // TODO: add command line processing here
  108.         // First argument is the server, second is the file to scan
  109.         // TODO: more rigourous argument checking
  110.         CString args = CString(m_lpCmdLine);
  111.  
  112.         int index = args.Find(' ');
  113.         if(index == -1)
  114.             exit(1);
  115.  
  116.         CString server = args.Left(index);
  117.          const CString fileName = args.Mid(index + 1);
  118.         unsigned short port;
  119.  
  120.         index = server.Find(':');
  121.         if(index != -1) {
  122.             port = (unsigned short)atoi(server.Mid(index + 1));
  123.             server = server.Left(index);    
  124.         } else
  125.             port = DEFAULT_PORT;
  126.  
  127.         TRY {
  128.             clamServer = new ClamServer(server, port);
  129.         } CATCH(CException, c) {
  130.             AfxMessageBox("Can't establish a connection to " + server);
  131.             c->Delete();
  132.             exit(1);
  133.         }
  134.         END_CATCH
  135.  
  136.          CWinApp::OnFileNew();
  137.  
  138.         // TODO: set quarantine directory
  139.          
  140.          exit(clamServer->Scan(fileName, 0, (CMainFrame *)AfxGetMainWnd(), m_pMainWnd, TRUE, NULL) == TRUE);
  141.     } else {
  142.         // create a new (empty) document
  143.         OnFileNew();
  144.     }
  145.  
  146. #ifdef _DEBUG
  147.     afxTraceEnabled = TRUE;
  148. #endif
  149.  
  150.     options = new COptions();
  151.     recursive = TRUE;
  152.  
  153.     return TRUE;
  154. }
  155.  
  156. /////////////////////////////////////////////////////////////////////////////
  157. // CAboutDlg dialog used for App About
  158.  
  159. class CAboutDlg : public CDialog
  160. {
  161. public:
  162.     CAboutDlg();
  163.  
  164. // Dialog Data
  165.     //{{AFX_DATA(CAboutDlg)
  166.     enum { IDD = IDD_ABOUTBOX };
  167.     //}}AFX_DATA
  168.  
  169. // Implementation
  170. protected:
  171.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  172.     //{{AFX_MSG(CAboutDlg)
  173.         // No message handlers
  174.     //}}AFX_MSG
  175.     DECLARE_MESSAGE_MAP()
  176. };
  177.  
  178. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  179. {
  180.     //{{AFX_DATA_INIT(CAboutDlg)
  181.     //}}AFX_DATA_INIT
  182. }
  183.  
  184. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  185. {
  186.     CDialog::DoDataExchange(pDX);
  187.     //{{AFX_DATA_MAP(CAboutDlg)
  188.     //}}AFX_DATA_MAP
  189. }
  190.  
  191. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  192.     //{{AFX_MSG_MAP(CAboutDlg)
  193.         // No message handlers
  194.     //}}AFX_MSG_MAP
  195. END_MESSAGE_MAP()
  196.  
  197. // App command to run the dialog
  198. void CClamavApp::OnAppAbout()
  199. {
  200.     CAboutDlg aboutDlg;
  201.     aboutDlg.DoModal();
  202. }
  203.  
  204. /////////////////////////////////////////////////////////////////////////////
  205. // CClamavApp commands
  206.  
  207. int CClamavApp::ExitInstance() 
  208. {
  209.     // TODO: Add your specialized code here and/or call the base class
  210.     if(clamServer) {
  211.         delete clamServer;
  212.         clamServer = NULL;
  213.     }
  214.     if(options) {
  215.         delete options;
  216.         options = NULL;
  217.     }
  218.     WSACleanup();
  219.  
  220.     return CWinApp::ExitInstance();
  221. }
  222.  
  223. void CClamavApp::OnFileNew() 
  224. {
  225.     CWinApp::OnFileNew();
  226.  
  227.     // TODO: Add your command handler code here
  228.  
  229.     // Open connection to a different server
  230.     if(clamServer) {
  231.         delete clamServer;
  232.         clamServer = NULL;
  233.     }
  234.     TRY {
  235.         clamServer = new ClamServer;
  236.     } CATCH(CException, c) {
  237.         clamServer = NULL;
  238.         c->Delete();
  239.     }
  240.     END_CATCH
  241. }
  242.  
  243. void CClamavApp::OnFileOpen() 
  244. {
  245.     // TODO: Add your command handler code here
  246.  
  247.     CString newName;
  248.     if (!DoPromptFileName(newName, AFX_IDS_OPENFILE,
  249.       /*OFN_HIDEREADONLY |*/ OFN_FILEMUSTEXIST, TRUE, NULL))
  250.         return; // open cancelled
  251.  
  252.     // OpenDocumentFile(newName);
  253.     this->Scan(newName);
  254. }
  255.  
  256. // TODO: More than one scan happen at once but the progress bar gets confused
  257. // Need a new scanner class. Create a new instance everytime we scan something
  258. // Pass clamServer as a parameter
  259. //    Scanner *s = new Scanner(clamServer, 0, (CMainFrame *)AfxGetMainWnd(), m_pMainWnd, recursive, options->m_quarantineDir);
  260. //    if(s->clean())
  261. //        AfxMessageBox("No virus found in " + filename);
  262. // delete s;
  263.     
  264. void CClamavApp::Scan(const CString& filename)
  265. {
  266.     if(clamServer == NULL)
  267.         AfxMessageBox("You must connect to a clamd server first");
  268.     else if(clamServer->Scan(filename, 0, (CMainFrame *)AfxGetMainWnd(), m_pMainWnd, recursive, options->m_quarantineDir))
  269.         AfxMessageBox("No virus found in " + filename);
  270. }
  271.  
  272. void CClamavApp::OnSetOptions() 
  273. {
  274.     // TODO: Add your command handler code here
  275.     if(options->DoModal() == IDOK)
  276.         recursive = options->m_recursive;
  277. }
  278.